/*
Copyright [2023] [fuwen_soft]
*/
package vip.xiaonuo.sys.modular.sysappinfoservice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import vip.xiaonuo.core.consts.CommonConstant;
import vip.xiaonuo.core.enums.CommonStatusEnum;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.core.util.PoiUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.sys.modular.sysappinfoservice.entity.SysAppInfoService;
import vip.xiaonuo.sys.modular.sysappinfoservice.enums.SysAppInfoServiceExceptionEnum;
import vip.xiaonuo.sys.modular.sysappinfoservice.mapper.SysAppInfoServiceMapper;
import vip.xiaonuo.sys.modular.sysappinfoservice.param.SysAppInfoServiceParam;
import vip.xiaonuo.sys.modular.sysappinfoservice.service.SysAppInfoServiceService;

import javax.annotation.Resource;
import java.util.List;

/**
 * APP服务简介service接口实现类
 *
 * @author yanwei
 * @date 2023-03-21 12:07:26
 */
@Service
public class SysAppInfoServiceServiceImpl extends ServiceImpl<SysAppInfoServiceMapper, SysAppInfoService> implements SysAppInfoServiceService {

    @Override
    public PageResult<SysAppInfoService> page(SysAppInfoServiceParam sysAppInfoServiceParam) {
        QueryWrapper<SysAppInfoService> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(sysAppInfoServiceParam)) {

            // 根据服务标题 查询
            if (ObjectUtil.isNotEmpty(sysAppInfoServiceParam.getServiceHead())) {
                queryWrapper.lambda().eq(SysAppInfoService::getServiceHead, sysAppInfoServiceParam.getServiceHead());
            }
            // 根据服务内容 查询
            if (ObjectUtil.isNotEmpty(sysAppInfoServiceParam.getServiceContent())) {
                queryWrapper.lambda().eq(SysAppInfoService::getServiceContent, sysAppInfoServiceParam.getServiceContent());
            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<SysAppInfoService> list(SysAppInfoServiceParam sysAppInfoServiceParam) {
        return this.list();
    }

    @Override
    public void add(SysAppInfoServiceParam sysAppInfoServiceParam) {
        SysAppInfoService sysAppInfoService = new SysAppInfoService();
        BeanUtil.copyProperties(sysAppInfoServiceParam, sysAppInfoService);
        this.save(sysAppInfoService);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<SysAppInfoServiceParam> sysAppInfoServiceParamList) {
        sysAppInfoServiceParamList.forEach(sysAppInfoServiceParam -> {
            this.removeById(sysAppInfoServiceParam.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SysAppInfoServiceParam sysAppInfoServiceParam) {
        SysAppInfoService sysAppInfoService = this.querySysAppInfoService(sysAppInfoServiceParam);
        BeanUtil.copyProperties(sysAppInfoServiceParam, sysAppInfoService);
        this.updateById(sysAppInfoService);
    }

    @Override
    public SysAppInfoService detail(SysAppInfoServiceParam sysAppInfoServiceParam) {
        return this.querySysAppInfoService(sysAppInfoServiceParam);
    }

    /**
     * 获取APP服务简介
     *
     * @author yanwei
     * @date 2023-03-21 12:07:26
     */
    private SysAppInfoService querySysAppInfoService(SysAppInfoServiceParam sysAppInfoServiceParam) {
        SysAppInfoService sysAppInfoService = this.getById(sysAppInfoServiceParam.getId());
        if (ObjectUtil.isNull(sysAppInfoService)) {
            throw new ServiceException(SysAppInfoServiceExceptionEnum.NOT_EXIST);
        }
        return sysAppInfoService;
    }

    @Override
    public void export(SysAppInfoServiceParam sysAppInfoServiceParam) {
        List<SysAppInfoService> list = this.list(sysAppInfoServiceParam);
        PoiUtil.exportExcelWithStream("SnowySysAppInfoService.xls", SysAppInfoService.class, list);
    }

}
